<?php
$this->useClass("Handler_Comments");
class Action_Community_AjaxComments extends Handler_Comments
{

	var $ajax_functions = array(
	'ajax_getComments',
	'ajax_getSubmitForm',
	'ajax_getChildsOfComment',
	'ajax_getComment',
	'ajax_getUpdateForm',
	'ajax_upCommentBody',
	'ajax_delComment',
	);

	public function ajax_delComment( $comment_oid ) {
		$cp = &$this->rh->ctx->cp;

		// проверка прав, пока просто проверим есть ли у него права на чтение, а вообще тут наверное должна быть более специфическая проверка
		$operations = $this->aa->getAvailableOperations($comment_oid);
		if ( !$this->aa->is_granted( array( 'comment_delete') ) ) {
			$this->rh->ctx->cp->set_data( "access denied" );
			$this->shutdown();
			return false;
		}

		$status = $this->deleteComment( $comment_oid );
		if ($status == COMMENT_OK) {
			$cp->set_data($comment_oid);
		} else {
			$cp->set_data("-1");
		}
	}

	public function ajax_upCommentBody( $comment_oid, $new_text ) {
		$cp = &$this->rh->ctx->cp;

		// проверка прав, пока просто проверим есть ли у него права на чтение, а вообще тут наверное должна быть более специфическая проверка
		$status = $this->updateCommentSmart($comment_oid, $new_text, $this->data['user_id']);

		if ($status == 0) {
			$cp->set_data($comment_oid);
		} else {
			$cp->set_data( "error N".$status );
		}

	}

	public function ajax_getComment( $comment_oid, $wrapped = false ) {

		$cp = &$this->rh->ctx->cp;
		// проверка прав
		$operations = $this->aa->getAvailableOperations($comment_oid);

		if ( !$this->aa->is_granted( array('item_read') ) ) {
			$this->rh->ctx->cp->set_data( "access denied" );
			return false;
		}
		$_cm = $this->getOneComment( $comment_oid );
		$_cm['oid'] = $comment_oid;

		$view = $this->rh->views->findByName($wrapped ? 'comments_loader_container' : 'comment_item');
		$view->data = $this->aa->template_data;
		$view->data['can_comment'] = $this->aa->is_granted(array('comment_create', 'comment_create_premod'));
		$view->data['*'] = $_cm;
		$view->data['item_oid'] = $_cm['item_oid'];
		$html = &$cp->add_node('html');
		$oid = &$cp->add_node('comment_oid');
		$html->set_data( $view->getContent() );
		$oid->set_data( $comment_oid );

	}

	public function ajax_getChildsOfComment( $parent_comment_oid, $item_oid, $offset ) {

		// проверка прав
		$operations = $this->aa->getAvailableOperations($item_oid);
		if ( !$this->aa->is_granted( array('item_read') ) ) {
			$this->rh->ctx->cp->set_data( "access denied" );
			return false;
		}

		$this->rh->UseClass("AggregatedModel_TreeComments");

		$this->m_tc->setTCparams();
		list($comments, $loader_type2) = $this->m_tc->getComments( $parent_comment_oid, $item_oid, $offset );
		foreach ($comments as $i=>$com) {
			$comments[ $i ]['acl'] = $this->aa->getAcl( $com['oid'] );
		}

		$cp = &$this->rh->ctx->cp;

		$nd_comments = &$cp->add_node('comments');
		$nd_form = &$cp->add_node('submit_form');
		$nd_comments_count = &$cp->add_node('comments_count');
		$nd_parent_oid = &$cp->add_node('parent_oid');
		$nd_item_oid = &$cp->add_node('item_oid');

		$view = $this->rh->views->findByName("comments_loader_container");
		$view->data = $this->aa->template_data;
		$view->data['comments'] = array( "items"=>$comments, "count"=>count($comments) );
		$view->data['loader_type2'] = $loader_type2;
		$view->data['can_comment'] = $this->aa->is_granted(array('comment_create', 'comment_create_premod'));

		$nd_item_oid->set_data( $item_oid );
		$nd_parent_oid->set_data( $parent_comment_oid );
		$nd_comments->set_data( $view->getContent() );
		$nd_comments_count->set_data( count($comments) );
	}

	public function ajax_getUpdateForm( $comment_oid ) {
		$cp = &$this->rh->ctx->cp;

		if ($comment_oid) {
			$_cm = $this->getOneComment( $comment_oid );
			$_cm['oid'] = $comment_oid;

			$view_f = $this->rh->views->findByName('comments_upform');
			$view_f->data = $this->aa->template_data;
			$view_f->data['can_comment'] = $this->aa->is_granted(array('comment_create', 'comment_create_premod'));
			$view_f->data['user']['date'] = RL_CyrDate::newNow();
			$view_f->data['user']['oid'] = $this->data['user_id'];
			$view_f->data['user']['url'] = $this->data['user_id'];
			$view_f->data['user']['avatar'] = $this->data['user']['avatar'];
			$view_f->data['comment_oid'] = $comment_oid;
			$view_f->data['comment'] = $_cm;

			$nd_comment_oid = &$cp->add_node('comment_oid');
			$nd_comment_oid->set_data($comment_oid);
			$cp->set_data( $view_f->getContent() );
		} else {
			$cp->set_data('1');
		}
	}

	public function ajax_getSubmitForm( $item_oid, $parent_comment_oid=NULL ) {
		$cp = &$this->rh->ctx->cp;

		$operations = $this->aa->getAvailableOperations($item_oid);

		if ( !$this->aa->is_granted( array('comment_create', 'comment_create_premod') ) ) {
			$cp->set_data( "access denied" );
			return false;
		}

		$nd_form = &$cp->add_node('submit_form');

		$_cm = NULL;
		if (isset($parent_comment_oid)) {
			$_cm = $this->getOneComment( $parent_comment_oid );
			$_cm['oid'] = $parent_comment_oid;
		}
		$view_f = $this->rh->views->findByName('commentsform');
		$view_f->data = $this->aa->template_data;
		$view_f->data['hide_comments_block'] = 1;
		$view_f->data['can_comment'] = $this->aa->is_granted(array('comment_create', 'comment_create_premod'));
		$view_f->data['user']['date'] = RL_CyrDate::newNow();
		$view_f->data['user']['oid'] = $this->data['user_id'];
		$view_f->data['user']['avatar'] = $this->data['user']['avatar'];
		$view_f->data['user']['url'] = $this->data['user_id'];
		$view_f->data['item_oid'] = $item_oid;
		$view_f->data['parent'] = $_cm;
		$nd_form->set_data( $view_f->getContent() );
	}

	public function ajax_getComments( $item_oid ) {
		$cp = &$this->rh->ctx->cp;

		// проверка прав
		$operations = $this->aa->getAvailableOperations($item_oid);

		$res = $this->aa->is_granted( array('item_read') );

		if ( !$res ) {
			$this->rh->ctx->cp->set_data( "access denied" );
			return false;
		}

		$this->m_tc->setTCparams(NULL, NULL);

		list($comments, $loader_type2) = $this->m_tc->getComments( $item_oid, $item_oid );

		foreach ($comments as $i=>$com) {
			$comments[ $i ]['acl'] = $this->aa->getAcl( $com['oid'] );
		}

		$nd_comments = &$cp->add_node('comments');
		$nd_form = &$cp->add_node('submit_form');
		$nd_comments_count = &$cp->add_node('comments_count');

		$view = $this->rh->views->findByName('comments_loader_container');
		$view->data = $this->aa->template_data;
		$view->data['comments'] = array( "items"=>$comments, "count"=>count($comments) );
		$view->data['loader_type2'] = $loader_type2;
		$view->data['item_oid'] = $item_oid;
		$view->data['can_comment'] = $this->aa->is_granted(array('comment_create_premod', 'comment_create'));

		if ( !isset($view->data['community']) ) {
			$item = $this->aa->store->getObject($item_oid);
			if ( isset($item->data) && !empty($item->data)
			&& isset($item->data['community_oid'])
			&& !empty($item->data['community_oid']) ) {
				$community = $item = $this->aa->store->getObject($item->data['community_oid']);

				$view->data['community'] = $community->data;
				$view->data['community']['url'] = $view->data['community']['community_url'];
			}
		}

		$nd_comments->set_data( $view->getContent() );
		$nd_comments_count->set_data( count($comments) );

		if ( $this->aa->is_granted( array( 'comment_create') ) ) {
			$view_f = $this->rh->views->findByName('commentsform');
			$view_f->data = $this->aa->template_data;
			$view_f->data['user']['oid'] = $this->data['user_id'];
			$view_f->data['user']['url'] = $this->data['user_id'];
			$view_f->data['user']['avatar'] = $this->data['user']['avatar'];
			$view_f->data['user']['date'] = RL_CyrDate::newNow();
			$view_f->data['item_oid'] = $item_oid;
			$nd_form->set_data( $view_f->getContent() );
		}
		//var_dump($view->getContent(), $view_f->getContent());

	}

	protected function init() {
		parent::init();
		$this->aa = $this->module->aa;
		$this->ctx->db->begin();

		$this->data = $this->aa->getInfo();
		$this->ajustSkin();
	}

	public function ajustSkin ()
	{
		if (!isset($this->params['community_url'])) {
			// -- old theme HACK : 2008-05-21 : lucky -- {{{
			$community_skin = 'platonica';
			$tpl = $this->ctx->tpl;
			$community_model = $this->ctx->mm->getModel("Community");
			$community_model->setDefaultSkin($community_skin);
			$tpl->setSkin($community_skin);
			$views_php = $tpl->findFile('', 'views', $tpl->_skin, 'php');
			unset ($config);
			include $views_php; // got $config
			$this->ctx->rh->views->setViewConfig($config);
			// -- old theme HACK : 2008-05-21 : lucky -- }}}
		}
	} // end of method ajustSkin

	protected function shutdown() {
		$this->ctx->db->end();
	}

	public function process ()
	{

	} // end of function execute

}
